package com.lty.jeeg.server.modules.tms.service.impl;

import com.lty.jeeg.server.common.page.table.PageTableHandler;
import com.lty.jeeg.server.common.page.table.PageTableRequest;
import com.lty.jeeg.server.common.page.table.PageTableResponse;
import com.lty.jeeg.server.common.utils.UserUtil;
import com.lty.jeeg.server.modules.sys.model.Notice;
import com.lty.jeeg.server.modules.sys.service.NoticeService;
import com.lty.jeeg.server.modules.tms.dao.TaskDao;
import com.lty.jeeg.server.modules.tms.model.TaskAtUsers;
import com.lty.jeeg.server.modules.tms.service.TaskAtUsersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lty.jeeg.server.modules.tms.dao.MattersDao;
import com.lty.jeeg.server.modules.tms.model.Matters;
import com.lty.jeeg.server.modules.tms.service.MattersService;

import java.util.List;

/**
 * @author : LTY
 * @date : 2019-04-03 15:00:28
 * Description : 
 */
@Service
@Slf4j
public class MattersServiceImpl implements MattersService {

    private final MattersDao mattersDao;

    private final TaskAtUsersService taskAtUsersService;

    private final NoticeService noticeService;

    private final TaskDao taskDao;

    @Autowired
    public MattersServiceImpl(MattersDao mattersDao, TaskAtUsersService taskAtUsersService, NoticeService noticeService, TaskDao taskDao) {
        this.mattersDao = mattersDao;
        this.taskAtUsersService = taskAtUsersService;
        this.noticeService = noticeService;
        this.taskDao = taskDao;
    }

    @Override
    public Matters getById(Long id) {
        return mattersDao.getById(id);
    }

    @Override
	@Transactional(rollbackFor = Exception.class)
	public int delete(Long id) {
		log.debug("删除Matters=>{}", id);
		return mattersDao.delete(id);
	}

    @Override
	@Transactional(rollbackFor = Exception.class)
	public int update(Matters matters) {
        Long mattersId = matters.getId();
        matters.preUpdate();
        log.debug("修改Matters=>{}", mattersId);

        // 删除@Users
        taskAtUsersService.deleteByTaskIdAndMattersId(matters.getTaskId(), mattersId);

        // 保存@Users
        List<Long> atUsers = matters.getAtUsers();
        if (atUsers != null) {
            for (Long userId : atUsers) {
                taskAtUsersService.save(new TaskAtUsers(matters.getTaskId(), mattersId, userId));
            }
        }


        return mattersDao.update(matters);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long save(Matters matters) {
        matters.preInsert();
        mattersDao.save(matters);
        log.debug("新增Matters=>{}", matters.getId());

        // 保存@Users
        Long mattersId = matters.getId();
        List<Long> atUsers = matters.getAtUsers();
        if (atUsers != null) {
            for (Long userId : atUsers) {
                taskAtUsersService.save(new TaskAtUsers(matters.getTaskId(), mattersId, userId));
                noticeService.save(new Notice("系统通知",
                        UserUtil.getCurrentUser().getNickname() + "在" + taskDao.getProjectNameAndTaskName(matters.getTaskId()) + "的任务@了您",
                        1, userId.toString(), null));
            }
        }

        return mattersId;
    }

    @Override
    public PageTableResponse list(PageTableRequest request) {
        return new PageTableHandler(
                request1 -> mattersDao.count(request1.getParams()),
                request2 -> mattersDao.list(
                        request2.getParams(), request2.getOffset(), request2.getLimit()))
                .handle(request);
    }

    @Override
    public List<Matters> listAll(Long taskId) {
        return mattersDao.listAll(taskId);
    }
}
